var usersDao = require('../DAO/users');
var ConstCode = require('../constant/ConstCode');

async function register(username, pwd, email) {
  var hasUsers = await usersDao.selectUsersByName(username);
  if (hasUsers.length > 0) {
    return { code: ConstCode.ERROR_CODE, msg: '用户已存在' };
  }
  await usersDao.register(username, pwd, email);
  return { code: ConstCode.SUCCESS_CODE };
}

async function login(username, password) {
  var users = await usersDao.selectUsersByPwdAndName(username, password);
  return users;
}

async function getUserById(userid) {
  var users = await usersDao.selectUserById(userid);
  return users;
}

module.exports = {
  register,
  login,
  getUserById
}